package com.ifast.common.controller;


import java.util.Arrays;

import com.ifast.sys.domain.CostDO;
import com.ifast.sys.service.CostService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.base.AdminBaseController;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 费用表
 * </pre>
 * @author  chenwenchao
 * @date  2018-09-18 10:17:20
 * <small> 2018-09-18 10:17:20 | chenwenchao</small>
 */
@Controller
@RequestMapping("/ifast/cost")
public class CostController extends AdminBaseController {
	@Autowired
	private CostService costService;
	
	@GetMapping()
	String Cost(){
	    return "ifast/cost/cost";
	}
	
	@ResponseBody
	@GetMapping("/list")
	public Result<Page<CostDO>> list(CostDO costDTO){
        Wrapper<CostDO> wrapper = new EntityWrapper<CostDO>().orderBy("id", false);
        Page<CostDO> page = costService.selectPage(getPage(CostDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	String add(){
	    return "ifast/cost/add";
	}

	@GetMapping("/edit/{id}")
	String edit(@PathVariable("id") Integer id,Model model){
		CostDO cost = costService.selectById(id);
		model.addAttribute("cost", cost);
	    return "ifast/cost/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	public Result<String> save( CostDO cost){
		costService.insert(cost);
        return Result.ok();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	public Result<String>  update( CostDO cost){
		boolean update = costService.updateById(cost);
		return update ? Result.ok() : Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	public Result<String>  remove( Integer id){
		costService.deleteById(id);
        return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		costService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
